﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BProdSkuProduct
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/9 8:33:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BProdSkuProduct : IProdSkuProduct
{
    public POProdSkuProduct CreateProdSkuProduct(long skuId, long productId, long materialId, DateTime? mdTime)
    {
        var result = new POProdSkuProduct
        {
            Id = XID.Nextval,
            ProdIndex = 1,
            SkuId = skuId,
            ProductId = productId,
            MaterialId = materialId,
            MdTime = mdTime
        };

        var db = Db.Default;

        db.ProdSkuProducts.Insert(result);
        return result;
    }

    public POProdSkuProduct DeleteProdSkuProduct(long? skuId)
    {

        var db = Db.Default;
        var result = GetProdSkuProductBySkuId(skuId);
        if (result != null)
        {
            db.ProdSkuProducts.Remove(result);
            var num = db.SaveChanges();
            if (num == 0)
            {
                return null;
            }
            return result;
        }
        return result;
    }
    public POProdSkuProduct DeleteProdSkuProductByProductId(long? productId)
    {

        var db = Db.Default;
        var result = GetProdSkuProductByProductId(productId);
        if (result != null)
        {
            db.ProdSkuProducts.Remove(result);
            var num = db.SaveChanges();
            if (num == 0)
            {
                return null;
            }
            return result;
        }
        return result;
    }

    public POProdSkuProduct GetProdSkuProductBySkuId(long? skuId)
    {
        var db = Db.Default;
        var result = db.ProdSkuProducts.Where(t => t.SkuId == skuId).FirstOrDefault();
        return result;
    }
    public POProdSkuProduct GetProdSkuProductByProductId(long? productId)
    {
        var db = Db.Default;
        var result = db.ProdSkuProducts.Where(t => t.ProductId == productId).FirstOrDefault();
        return result;
    }
    public List<POProdSkuProduct> GetProdSkuProductBySkuIds(List<long> skuIds)
    {
        var db = Db.Default;
        var skuProducts = db.ProdSkuProducts.ToList();
        var result = skuProducts.Where(t => skuIds.Contains(t.SkuId ?? 0)).ToList();
        return result;
    }
    //public List<POProdSkuProduct> GetInWarehouseProdSkuProductBySkuIds(List<long> skuIds)
    //{
    //    var db = Db.Default;
    //    var skuProducts = db.ProdSkuProducts.Where(t => t.OutBoundFlag == 0).ToList();
    //    var result = skuProducts.Where(t => skuIds.Contains(t.SkuId ?? 0)).ToList();
    //    return result;
    //}

    public List<long?> GetProdSkuProductBySkuIdsAndMaterial(List<long> skuIds, long materialId)
    {
        var db = Db.Default;
        var skuProducts = db.ProdSkuProducts.Where(t => t.OutBoundFlag == 0 && t.MaterialId == materialId).Select(t => t.SkuId).ToList();
        var result = skuProducts.Join(skuIds, f => f, s => s, (f, s) => f).ToList();

        //var result = skuProducts.Where(t => skuIds.Contains(t.SkuId ?? 0)).ToList();
        return result;
    }

    public List<POProdSkuProduct> GetProdSkuProducts()
    {
        var db = Db.Default;
        var result = db.ProdSkuProducts.ToList();
        return result;
    }
    public POProdSkuProduct GetProdSkuProductByProductId(long productId)
    {
        var db = Db.Default;
        var skuProduct = db.ProdSkuProducts.Where(t => t.ProductId == productId).OrderByDescending(t => t.CreateTime).FirstOrDefault();
        return skuProduct;
    }
    public int UpdateSkuProductMaterial(long productId, long mateiralId)
    {
        var db = Db.Default;
        var result = db.ProdSkuProducts.Where(t => t.ProductId == productId).Update( new POProdSkuProduct()
        {
            MaterialId = mateiralId,
            UpdateTime = DateTime.Now,
        });
        return db.SaveChanges();
    }

    public int UpdateSkuProductOutBoundFlag(long skuId)
    {

        var db = Db.Default;

        var result = db.ProdSkuProducts.Where(t => t.SkuId == skuId)
             .Update( new POProdSkuProduct
             {
                 OutBoundFlag = 1,
             });
        return db.SaveChanges();

        //var prod = db.ProdSkuProducts.Where(T => T.SkuId == skuId).FirstOrDefault();
        //if (prod != null)
        //{
        //    prod.OutBoundFlag = 1;
        //    try
        //    {
        //        return db.SaveChanges();
        //    }
        //    catch (Exception)
        //    {
        //        //TODO 
        //    }


        //}

    }
}
